package com.alpha.eceasy.mall.service.impl;

import com.alpha.eceasy.extension.BaseServiceImpl;
import com.alpha.eceasy.extension.BaseFormInfo;
import com.alpha.eceasy.mall.api.entity.MallCommodity;
import com.alpha.eceasy.mall.api.entity.MallCommoditySpecification;
import com.alpha.eceasy.mall.api.vo.MallCommodityCategoryAssoVO;
import com.alpha.eceasy.mall.api.vo.MallCommoditySpecificationVO;
import com.alpha.eceasy.mall.api.vo.MallCommodityVO;
import com.alpha.eceasy.mall.mapper.MallCommodityMapper;
import com.alpha.eceasy.mall.service.MallCommodityCategoryAssoService;
import com.alpha.eceasy.mall.api.entity.MallCommodityCategoryAsso;
import com.alpha.eceasy.mall.mapper.MallCommodityCategoryAssoMapper;
import com.alpha.eceasy.mall.service.MallCommodityCategoryService;
import com.alpha.eceasy.mall.service.MallCommodityService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

@Service
public class MallCommodityCategoryAssoServiceImpl extends BaseServiceImpl<MallCommodityCategoryAssoMapper, MallCommodityCategoryAsso>
        implements MallCommodityCategoryAssoService {
    @Resource
    private MallCommodityMapper commodityMapper;
    @Resource
    private MallCommoditySpecificationServiceImpl commoditySpecificationService;

    @Override
    public IPage<MallCommoditySpecificationVO> getMallCommoditySpecificationVOPage(Page<MallCommodityCategoryAsso> page,
                                                                                   MallCommodityCategoryAsso entity) {
        List<MallCommoditySpecificationVO> voList = this.list(Wrappers.lambdaQuery(entity))
                .stream().map(asso -> {
                    MallCommodity commodity = commodityMapper.selectByGuid(asso.getCommodityGuid());
                    if (commodity == null) {
                        return null;
                    }
                    MallCommoditySpecification commoditySpecification = commoditySpecificationService.getOne(Wrappers.<MallCommoditySpecification>lambdaQuery()
                            .eq(MallCommoditySpecification::getCommodityGuid, commodity.getGuid()), false);
                    if (commoditySpecification == null) {
                        return null;
                    }
                    return commoditySpecificationService.getCommoditySpecificationVO(commoditySpecification);
                }).filter(Objects::nonNull).toList();
        IPage<MallCommoditySpecificationVO> voPage = page.convert(vo -> new MallCommoditySpecificationVO());
        voPage.setRecords(voList);
        return voPage;
    }
}
